from tkinter import *
from tkinter.messagebox import *
from tkinter import ttk
from tkcalendar import DateEntry
from dev.serialList import *
from pages.chartPageT10 import chartPageT10
from pages.chartPageA8 import chartPageA8

class ConnectFrame(object):  # 继承Frame类
    def __init__(self, master=None,page=None):
        # Frame.__init__(self, master)
        self.root = master  # 定义内部变量root
        self.page = page
        self.port = StringVar()
        self.baudrate = IntVar()
        self.devName = StringVar()
        self.slave = IntVar()
        self.baudrateRange = [9600, 19200, 38400, 57600, 115200]
        self.serialList = list_serial_ports()
        self.createPage()

    def createPage(self):
        Label(self.page).grid(row=0, stick=W)
        # 设备型号选择
        Label(self.page, text='设备型号: ').grid(row=1, stick=W, pady=10)
        devComb = ttk.Combobox(self.page, textvariable=self.devName, values=('SD123-T10', 'ZSKJ-A8'), state='readonly', width=40)
        devComb.grid(row=1, column=1, stick=E)
        devComb.current(0)
        # 端口选择
        Label(self.page, text='端口: ').grid(row=2, stick=W, pady=10)
        portComb = ttk.Combobox(self.page, textvariable=self.port, values=self.serialList, state='readonly', width=40)
        portComb.grid(row=2, column=1, stick=E)
        portComb.current(0)
        # 波特率
        Label(self.page, text='波特率: ').grid(row=3, stick=W, pady=10)
        baudComb = ttk.Combobox(self.page, textvariable=self.baudrate, values=self.baudrateRange, state='readonly', width=40)
        baudComb.grid(row=3, column=1, stick=E)
        baudComb.current(0)
        # 从站号
        Label(self.page, text='从站号: ').grid(row=4, stick=W, pady=10)
        slaveComb = ttk.Combobox(self.page, textvariable=self.slave, values=[str(i) for i in range(1,247)], state='readonly', width=40)
        slaveComb.grid(row=4, column=1, stick=E)
        slaveComb.current(0)
        # 确定按钮
        Button(self.page, text='连接设备', command=self.connect).grid(row=5, stick=W, pady=10)
        Button(self.page, text='刷新设备', command=self.refresh).grid(row=5, column=1, stick=W, pady=10)
    def refresh(self):
        self.serialList = list_serial_ports()
        portComb = ttk.Combobox(self.page, textvariable=self.port, values=self.serialList, state='readonly', width=40)
        portComb.grid(row=2, column=1, stick=E)
        portComb.current(0)
    def connect(self):
        self.root.devName = self.devName.get()
        self.root.port = self.port.get().split('-')[0]
        self.root.baudrate = self.baudrate.get()
        self.root.slave = self.slave.get()
        if self.root.port == '无串口设备' :
            showinfo(title='提示', message='请连接设备')
        elif self.root.baudrate not in self.baudrateRange:
            showinfo(title='提示', message='波特率输入有误')
        elif self.root.devName == 'SD123-T10':
            self.cp = chartPageT10(self.root)
            self.cp.createPage()
            
        elif self.root.devName == 'ZSKJ-A8':
            self.cp = chartPageA8(self.root)
            self.cp.createPage()
        else:
            showinfo(title='提示', message='设备型号输入有误')
        return


class QueryFrame(object):  # 继承Frame类
    def __init__(self, master=None,page=None):
        self.root = master  # 定义内部变量root
        self.page = page
        self.devName = StringVar()
        self.createPage()

    def createPage(self):
        Label(self.page).grid(row=0, stick=W)
        # 设备型号选择
        Label(self.page, text='设备型号: ').grid(row=1, stick=W, pady=10)
        devComb = ttk.Combobox(self.page, textvariable=self.devName, values=('SD123-T10', 'ZSKJ-A8'), state='readonly', width=40)
        devComb.grid(row=1, column=1, stick=E)
        devComb.current(0)

        Label(self.page, text='开始时间: ').grid(row=2, stick=W, pady=10)
        report_cal = DateEntry(self.page, width=20, background='darkblue',foreground='white', borderwidth=2)
        report_cal.grid(row=2, column=1, stick=E)
        

'''
        Label(self.page, text='查询页面').grid(padx=20,pady=10)
        Label(self.page).grid(row=0, stick=W, pady=10)
        Label(self.page, text='请输入查询数据: ').grid(row=1, stick=W, pady=10)
        Entry(self.page, textvariable=self.itemName).grid(row=1, column=1, stick=E)
'''
class CountFrame(object):  # 继承Frame类
    def __init__(self, master=None,page=None):
        self.root = master  # 定义内部变量root
        self.page = page
        self.createPage()

    def createPage(self):

        Label(self.page, text='统计界面').grid(padx=20, pady=10)
        Label(self.page).grid(row=0, stick=W, pady=10)
        Label(self.page, text='统计结果: ').grid(row=1, stick=W, pady=10)


class AboutFrame(object):  # 继承Frame类
    def __init__(self, master=None,page=None):
        self.root = master  # 定义内部变量root
        self.page = page
        self.createPage()

    def createPage(self):
        Label(self.page, text='关于界面').grid(padx=20, pady=10)
        Label(self.page).grid(row=0, stick=W, pady=10)
        Label(self.page, text='关于: ').grid(row=1, stick=W, pady=10)

